require 'iconv'
require 'spreadsheet/excel'
include Spreadsheet
def file_name
   #"#{RAILS_ROOT}/tmp/" + user.username + "_timelogs_" + date_label + ".xls")
   "#{RAILS_ROOT}/tmp/timelogs.xls"
end
def gen_excel(timelogs) 
   workbook = Excel.new(file_name)
   worksheet = workbook.add_worksheet("Timesheet")

   format_title = Format.new(:color=>"black",:bold=>true,:underline=>true)
   format_foot  = Format.new(:color => "green", :bold => true,:underline => true)   

   worksheet.write(0,0,["Username","Project name","Start - End","hours","Desc","Confiremed by"],format_foot)

   ic = Iconv.new('latin1','utf-8')

   timelogs.each_with_index do |t,i|
     worksheet.write((i+1),0,[
       t.user.name,
       t.project.name,t.start_at.strftime("%Y/%m/%d %H:%M") + " - " + t.end_at.strftime("%Y/%m/%d %H:%M"),
       sprintf("%0.2f",(t.end_at - t.start_at)/3600),
       t.work_desc,
       (t.confirmed == 1 ? t.confirmed_user.name + " " + t.confirmed_at.strftime("%m/%d %H:%M") : "unconfirmed")
       ])
   end

   #worksheet2.write_column(4,4,[1,2,3])

   #worksheet3.write(2,2,"World",format3)
   #worksheet3.write(3,3,[[1,2,3],[4,5,6],[7,8,9]])

   #worksheet1.format_row(1,25,format)
   #worksheet2.format_column(0..2,30,format2)
   workbook.close
 end
